import vue from "@vitejs/plugin-vue";
import Components from "unplugin-vue-components/vite";
import { VantResolver } from "unplugin-vue-components/resolvers";
import postcsspxtoviewport from "postcss-px-to-viewport";
import I18N from "@intlify/unplugin-vue-i18n/vite";
import { defineConfig, loadEnv } from "vite";
import { resolve } from "path";

export default defineConfig(({ command, mode }) => {
  const root = process.cwd();
  const env = loadEnv(mode, root);
  return {
    root,
    base: env.VITE_APP_TITLE == 'pro-android' ? './' : '/',
    resolve: {
      alias: {
        "@": resolve(__dirname, "src"),
      },
    },
    server: {
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: env.VITE_APP_BASE_ONLINE_TARGET_API,
          changeOrigin: true,
          rewrite: (path) => path.replace(env.VITE_APP_BASE_API, ""),
        },
      },
    },
    plugins: [
      vue(),
      Components({
        resolvers: [VantResolver()],
      }),
      // i18n 国际化支持
      I18N({
        runtimeOnly: false,
        compositionOnly: true,
        include: [resolve(__dirname, "./locales/**")],
      }),
    ],
    css: {
      postcss: {
        plugins: [
          postcsspxtoviewport({
            viewportWidth: 390, // 视窗的宽度，对应的是我们设计稿的宽度，一般是750
            viewportHeight: 844, // 视窗的高度，根据750设备的宽度来指定，一般指定1334，也可以不配置
            viewportUnit: "vw", // 指定需要转换成的视窗单位，建议使用vw
          }),
        ],
      },
    },
  };
});
